/*
 * i3-ueb1_2013.c
 *
 *  Created on: 13.02.2013
 *      Author: Alex
 *
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include "polynom.h"
int i;

Polynom create_poly(Polynom pol, double koeff, int pot) {
	Polynom neuesElement= (Polynom) malloc(sizeof(struct polynom_struct));

	neuesElement->koeff = koeff;  	//
	neuesElement->pot = pot; //hier alles genau so wie auf http://perlgeek.de/de/artikel/einfach-verkettete-listen

	neuesElement->next = pol->next;
	pol->next = neuesElement;
	return pol;
}

double eval(double x, Polynom p1) {

	double stelle_ergebnis = 0, ergebniss = 0;
	double zwsumme = 1;
	int j = 1, i_while = 0;

	while (p1->next != NULL ) { //funktioniert nicht, sollte aber

		j = p1->next->pot; //da wir neuesElement->pot nicht dekrementieren duerfen
		while (j >= 1) { // diese while fuer X^ausrechnen
			zwsumme = zwsumme * x;
			j--;
		}

		stelle_ergebnis = p1->next->koeff * zwsumme; //hier wird ergebniss zuerst fuer erste Stelle dann zweite usw. gerechnet
		ergebniss = ergebniss + stelle_ergebnis;

		printf("(%2.1f*X^%i) + ", p1->next->koeff, p1->next->pot);
		p1 = p1->next;

		zwsumme = 1; //zwsumme muss man reseten sonst X^potenz wird falsch
		i_while++;

	}

	return ergebniss;
}

double eval2(double x, Polynom p1) {
	Polynom iter = (Polynom) malloc(sizeof(struct polynom_struct));
	iter = p1;
	double ergebnis = 0;
	double koeff2;
	int j = 0, k = 0, n = 1;

	while (iter->next != NULL ) {
		k++;
		iter = iter->next;
	}

	while (n <= k) {

		if (n >= k) {
			koeff2 = 0;
		} else
			koeff2 = p1->next->next->koeff;

		if (j == 0) {
			ergebnis = p1->next->koeff * x + koeff2;
			j = 1;
		}

		else
			ergebnis = ergebnis * x + koeff2;

		p1 = p1->next;
		n++;
	}
	return ergebnis * x;
}

Polynom add_poly(Polynom p1, Polynom p2) {
	Polynom p3 = (Polynom) malloc(sizeof(struct polynom_struct));
	Polynom p44 = (Polynom) malloc(sizeof(struct polynom_struct));
	Polynom p55 = (Polynom) malloc(sizeof(struct polynom_struct));
	p3->next = NULL;
	p44->next = NULL;
	p55->next = NULL;
	int i=1;

Polynom p22 = p2;
Polynom p11 = p1;

while (p11->next != NULL ) {
	p11 = p11->next;
		p55 = create_poly(p44,p11->koeff, p11->pot);

}

	while (p55->next != NULL ) {
		p55 = p55->next;

		while (p2->next != NULL ) {
			p2 = p2->next;

			if (p55->pot == p2->pot) {

				create_poly(p3, p55->koeff + p2->koeff, p55->pot);
i++;
			}

		}
p2 = p22;
	}

	return p3;
}

int main() {

	Polynom p1, p2;
	p1 = (Polynom) malloc(sizeof(struct polynom_struct));
	p2 = (Polynom) malloc(sizeof(struct polynom_struct));

	p1->next = NULL;
	p2->next = NULL;

	int x = 2;

	p1 = create_poly(p1, 4, 2);
	p1 = create_poly(p1, 7, 3);
	p1 = create_poly(p1, 2, 4);

	p2 = create_poly(p2, 5, 2);
	p2 = create_poly(p2, 5, 3);
	p2 = create_poly(p2, 3, 4);

	printf("\n  Polynom f(%d)= %f \n", x, eval(x, p1));
	printf("\n  Polynom f(%d)= %f \n", x, eval2(x, p1));

	printf("\n  Polynom f(%d)= %f \n", x, eval(x, p2));
	printf("\n  Polynom f(%d)= %f \n", x, eval2(x, p2));


	printf("\n  Polynom f(%d)= %f \n", x, eval(x, add_poly(p1, p2)));
	printf("\n  Polynom f(%d)= %f \n", x, eval2(x, add_poly(p1, p2)));



	free(p1);

	return 0;

}

